/*
 * Copyright (C) 2011 Consulanza Informatica.
 * info@consulanza.it
 *
 * This file is part of Customer Calls Tracker.
 *
 *    Customer Calls Tracker is free software: you can redistribute it and/or
 *    modify it under the terms of the GNU General Public License as published
 *    by the Free Software Foundation, either version 3 of the License, or (at 
 *    your option) any later version.
 *
 *    Customer Calls Tracker is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 *    Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Customer Calls Tracker. If not, see http://www.gnu.org/licenses/.
 *
 */

package consulanza.amlsoft.customercalls;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import android.text.format.DateFormat;
import android.util.Log;

public class Util {
	private static final String TAG = "CustomerCalls Util";
	/**
	 * Convert integer to boolean
	 * @param value
	 * @return
	 */
	public static boolean int2bool(int value) {
    	return (value > 0);
    }
    public static int bool2int(boolean value) {
    	if (value) {
    		return 1;
    	} else {
    		return 0;
    	}
    }
	/**
	 * Return default date format
	 * @return
	 */
    public static SimpleDateFormat iso8601Format() {
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    	return format;
    }
    /**
     * Restituisce data e ora corrente in formato stringa iso8601
     * @return current time
     */
    public static String nowToString() {
    	Date date = new Date();
    	return iso8601Format().format(date);
    }
    /**
     * Restituisce un valore di tipo Date da una data in formato testo iso8601
     * @param dbDate
     * @return date
     */
    public static Date dbDateToDate(String dbDate) {
    	Date date = null;
    	try {
    		date = iso8601Format().parse(dbDate);
   		} catch (Exception e) {
			Log.e(TAG, "Parsing ISO8601 datetime failed", e);
   		}
    	return date;
    }
    /**
     * Restituisce il giorno della settimana
     * @param dbDate
     * @return
     */
    public static String dateToDow(Date date) {
    	final DateFormat df = new DateFormat();
    	return df.format("E",date).toString();
    }
    /**
     * Restituisce data e ora come stringa formattata in base alle impostazioni del telefono
     * @param dbDate
     * @return
     */
    public static String dateToString(Date date) {
    	return SimpleDateFormat.getDateTimeInstance().format(date);
    }
    /**
     * 
     * @param sDate1
     * @param sDate2
     * @return
     */
    public static String timeDiff(String sDate1, String sDate2) {
    	Date date1 = dbDateToDate(sDate1);
    	Date date2 = dbDateToDate(sDate2);
    	long diffInMis = (date2.getTime() - date1.getTime());
    	long diffInMin = TimeUnit.MILLISECONDS.toSeconds(diffInMis) /60;
    	
    	long diffH = diffInMin / 60;
    	long diffM = diffInMin % 60;
    	
    	String dateString = String.format("%02d:%02d",diffH,diffM); 
    	return dateString;
    }

}
